package mysql

import (
	"context"
	"data-transfer/domain"
	"data-transfer/global"
	"data-transfer/plugins"
	"data-transfer/transfer"
	"go.uber.org/zap"
	"sync/atomic"
	"time"
)

//InsertArticleNews 插入关联的资讯信息到InfoService的Article表
func (mdb *db) InsertArticleNews(ctx context.Context, t plugins.TargetStore) (int64, error) {
	var (
		affected int64
		err      error
		rows     []*domain.Articles
	)

	news, err := t.GetNews(ctx)
	if err != nil {
		global.LOG.Error("获取 【NEWS】 数据出错:", zap.Any("err", err))
	}
	for _, n := range news {
		skId := n.Id
		columnLevel := global.ColumnLevel.News

		r := &domain.Articles{
			SkId:        skId,
			ColumnLevel: columnLevel,
			SortDate:    time.Now(),
		}

		/*
				aff, e :=  mdb.db.Exec(transfer.INSERT_ARTICLE_SQL, r.ColumnLevel,r.SkId) //执行原生SQL
				if e != nil {
					err = e
					global.LOG.Error("执行INSERT_ARTICLE出错:", zap.Any("err", err))
				}

				aft,e := aff.RowsAffected()
				if e != nil {
					err = e
					global.LOG.Error("执行INSERT_ARTICLE出错:", zap.Any("err", err))
				}

			affected += aft
		*/
		rows = append(rows, r) //构建Article切片，并发插入使用
	}

	//并发执行闭包函数
	execFun := func(index int) {
		r := rows[index]
		aff, e := mdb.db.Exec(transfer.INSERT_ARTICLE_SQL, r.SortDate, r.ColumnLevel, r.SkId) //执行原生SQL
		if e != nil {
			err = e
			global.LOG.Error("执行INSERT_ARTICLE出错:", zap.Any("err", e))
		}

		aft, e := aff.RowsAffected()
		if e != nil {
			err = e
			global.LOG.Error("执行INSERT_ARTICLE出错:", zap.Any("err", e))
		}
		atomic.AddInt64(&affected, aft)
	}

	DoBatch(len(rows), 20, execFun)

	return affected, err

}

//InsertArticlesSingle 数据监控插入关联的资讯、公告信息到InfoService的Article表
func (mdb *db) InsertArticlesSingle(articles *domain.Articles) (int64, error) {
	var err error
	result, err := mdb.db.Exec(transfer.INSERT_ARTICLE_SQL, articles.SortDate, articles.ColumnLevel, articles.SkId)
	if err != nil {
		return 0, err
	}
	affected, err := result.RowsAffected()
	return affected, err
}

//InsertArticleAnnouncements 插入关联的公告信息到InfoService的Article表
func (mdb *db) InsertArticleAnnouncements(ctx context.Context, t plugins.TargetStore) (int64, error) {
	var (
		affected int64
		err      error
		rows     []*domain.Articles
	)

	//获取所有的公告信息
	anmts, err := t.GetAnnouncements(ctx)
	if err != nil {
		global.LOG.Error("获取 【ANNOUNCEMENT】 数据出错:", zap.Any("err", err))
	}

	for _, a := range anmts {
		skId := a.Id
		columnLevel := global.ColumnLevel.Announcement

		r := &domain.Articles{
			SkId:        string(skId),
			ColumnLevel: columnLevel,
			SortDate:    time.Now(),
		}

		/*
			aff, e :=  mdb.db.Exec(transfer.INSERT_ARTICLE_SQL, r.ColumnLevel,r.SkId) //执行原生SQL
			if e != nil {
				err = e
				global.LOG.Error("执行INSERT_ARTICLE出错:", zap.Any("err", err))
			}

			aft,e := aff.RowsAffected()
			if e != nil {
				err = e
				global.LOG.Error("执行INSERT_ARTICLE出错:", zap.Any("err", err))
			}

			affected += aft
		*/
		rows = append(rows, r) //构建Article切片，并发插入使用
	}

	//并发执行闭包函数
	execFun := func(index int) {
		r := rows[index]
		aff, e := mdb.db.Exec(transfer.INSERT_ARTICLE_SQL, r.SortDate, r.ColumnLevel, r.SkId) //执行原生SQL
		if e != nil {
			err = e
			global.LOG.Error("执行INSERT_ARTICLE出错:", zap.Any("err", e))
		}

		aft, e := aff.RowsAffected()
		if e != nil {
			err = e
			global.LOG.Error("执行INSERT_ARTICLE出错:", zap.Any("err", e))
		}
		atomic.AddInt64(&affected, aft)
	}

	DoBatch(len(rows), 20, execFun)

	return affected, err
}
